package com.wnx.job.modules.rms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.common.mapstruct.IntentionMapper;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.model.PmsIndustry;
import com.wnx.job.modules.pms.service.PmsResumeService;
import com.wnx.job.modules.rms.dto.IntentionDto;
import com.wnx.job.modules.rms.dto.IntentionParam;
import com.wnx.job.modules.rms.dto.ResumeParam;
import com.wnx.job.modules.rms.model.RmsIntention;
import com.wnx.job.modules.rms.service.RmsIntentionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/rms/rmsIntention")
@Api(tags = "IntentionController",description = "求职意向管理")
public class RmsIntentionController {

    @Resource
    private RmsIntentionService intentionService;
    @Resource
    private JmsStudentService studentService;

    @Resource
    private PmsResumeService resumeService;


    /**
     * 获取学生求职意向列表
     * @param principal
     * @return
     */
    @GetMapping("/getIntentionList")
    @ApiOperation("获取学生求职意向列表")
    public ResultData<List<IntentionParam>> getIntentionList(Principal principal){
        log.info("========系统正在获取学生求职意向列表==========");

        //前端需要的行业集合
        List<String> intentionListFront = new ArrayList<String>();

        //通过认证主体，获取学生学号。
        String account = principal.getName();

        //调用服务层，根据学生学号获取学生信息
        JmsStudent student = studentService.findByAccount(account);

        //调用服务层，根据学生ID获取简历
        ResumeParam resume = resumeService.findByStudentId(student.getId());

        //调用服务层，根据简历ID获取简历求职意向集合
        List<RmsIntention> intentionList =  intentionService.findIntentionListByResumeId(resume.getId());

        List<IntentionParam> intentionParamList = IntentionMapper.INSTANCE.toDtoList(intentionList);


        //循环遍历求职意向集合
        intentionParamList.forEach(x -> {
            //调用服务层，根据求职意向ID获取简历行业集合
            List<PmsIndustry>  industryList = intentionService.findIndustryListById(x.getId());

            //设置进求职意向对象中
            x.setIndustryList(
                    industryList.stream()
                            .map(PmsIndustry::getName)
                            .collect(Collectors.toList()));

        });


        log.info("=========系统成功获取求职意向信息====================");
        return ResultData.success(Const.QUERY_INTENTION_SUCCESS,intentionParamList);

    }


    /**
     * 新增求职意向
     * @param intentionDto
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增求职意向")
    public ResultData<String> save(@Valid @RequestBody IntentionDto intentionDto, Principal principal){

        //后端需要的求职意向对象
        RmsIntention intention = new RmsIntention();
        //后端需要的行业集合
        List<Long> industryIds = new ArrayList<Long>();

        //通过认证主体，获取学生学号。
        String account = principal.getName();

        //调用服务层，根据学生学号获取学生信息
        JmsStudent student = studentService.findByAccount(account);

        //调用服务层，根据学生ID获取简历
        ResumeParam resume = resumeService.findByStudentId(student.getId());


        //属性复制
        intention.setName(intentionDto.getName());
        intention.setCity(intentionDto.getCity());
        intention.setSalary(intentionDto.getSalary());
        intention.setWorkType(StringUtils.join(intentionDto.getWorkType(),"/"));
        intention.setResumeId(resume.getId());

        //取出Dto行业属性，赋值给industryIds
        List<Long[]> industry = intentionDto.getIndustry();

        if (industry.size() <= 3){
            for (Long[] ids : industry) {
                industryIds.add(ids[1]);
            }
        }else {
            Asserts.fail("选择的行业大于3个，最多只能选择三个！");
        }


        //调用服务层添加求职意向
        intentionService.saveIntention(intention,industryIds);

        //返回结果集对象
        return ResultData.success(Const.ADD_INTENTION_SUCCESS);
    }


    /**
     * 删除求职意向
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除求职意向")
    public ResultData<String> deleteIntention(@PathVariable(name = "id")Long id){
        //获取前端的求职意向ID

        //调用服务层，根据ID删除求职意向
        intentionService.deleteIntention(id);

        //返回结果集对象
        return ResultData.success(Const.DELETE_INTENTION_SUCCESS);
    }




}

